#!/usr/bin/python

import os, sys


if not ((len(sys.argv) == 5) or (len(sys.argv) ==7 ) ):
	print 'mysql_get_smiles_smarts.py <path to directory with mysql_get_smiles_smarts.py> <query file> <file with smarts patterns> <output file (prefix)> <chunk size> <max rows> (last two parameters are optional)'
	print 'Note: smarts file must be of format: smarts tab id'
	sys.exit(1)
elif len(sys.argv) ==7:
	try:
		chunk_size = int(sys.argv[5])
		max_rows = int(sys.argv[6])
		chunks = True
	except:
		print 'limits must be integers'
		sys.exit(1)
else:
	chunks = False

path = sys.argv[1]
query = sys.argv[2]
smarts_file = sys.argv[3]
out_file = sys.argv[4]


file = '#$ -S /bin/tcsh\n#$ -cwd\n#$ -V\n' + path + '/mysql_get_smiles_smarts.py '

if not chunks:
	file_name = out_file + '_start.bin'
	start_file = open(file_name, 'w')
	start_file.write(file)
	start_file.write(query + ' ' + smarts_file + ' ' + out_file + '.smi')
	start_file.close()
	os.system('chmod 744 '  + file_name)
	command = 'qsub -q 64bit-pri.q ' + file_name
	print command
	os.system(command)

else:
	start = 0
	while start < max_rows:
		limit_start = start
		start = limit_start + chunk_size

		file_name = out_file + '_' + str(limit_start) + '_start.bin'
		start_file = open(file_name, 'w')
		start_file.write(file)
		start_file.write(query + ' ' + smarts_file + ' ' + out_file + '_' + str(limit_start) + '.smi' + ' ' + str(limit_start) + ' ' + str(chunk_size))
		start_file.close()
		os.system('chmod 744 '  + file_name)
		command = 'qsub ' + file_name
		print command
		os.system(command)

	
	
